package com.cityos.android.mystudynestedscroll.ui.view

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.view.ViewCompat
import com.cityos.android.mystudynestedscroll.common.utils.LoggerUtils
import com.cityos.android.mystudynestedscroll.common.utils.MotionEventPrinter

/**
 * @author hailong
 * @date 2024年03月04日 09:26
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 */
class MyView @JvmOverloads constructor(context: Context , attrs: AttributeSet? = null) : AppCompatTextView(context , attrs) {
//    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
//        LoggerUtils.justE("start---------->MyView.dispatchTouchEvent:${MotionEventPrinter.getEventNameStr(ev)}" , "MyView")
//        val a = super.dispatchTouchEvent(ev)
//        LoggerUtils.justE("end---------->MyView.dispatchTouchEvent = $a:${MotionEventPrinter.getEventNameStr(ev)}" , "MyView")
//        return a
//    }


//    override fun onTouchEvent(event: MotionEvent?): Boolean {
//        LoggerUtils.justE("start---------->MyView.onTouchEvent:${MotionEventPrinter.getEventNameStr(event)}" , "MyView")
//        var a = super.onTouchEvent(event)
//        a=true
//        LoggerUtils.justE("end---------->MyView.onTouchEvent = $a:${MotionEventPrinter.getEventNameStr(event)}" , "MyView")
//        return true
//    }


    private var lastX = 0f
    private var lastY = 0f

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        super.onTouchEvent(event)
        if (event == null) {
            return false
        }

        val x = event.getX()
        val y = event.getY()

        when (event.action) {
            MotionEvent.ACTION_DOWN                           -> {
                lastX = x
                lastY = y
            }

            MotionEvent.ACTION_MOVE                           -> {
                LoggerUtils.justE("lastX=${lastX} lastY=${lastY} currentX=${x} currentY=${y}")
                val offsetX = (x - lastX).toInt()
                val offsetY = (y - lastY).toInt()
                ViewCompat.offsetTopAndBottom(this , offsetY)
                ViewCompat.offsetLeftAndRight(this , offsetX)
//                layout(left + offsetX , top + offsetY , right + offsetX , bottom + offsetY)
            }

            MotionEvent.ACTION_UP , MotionEvent.ACTION_CANCEL -> {
                LoggerUtils.justE("lastX=${lastX} lastY=${lastY} currentX=${x} currentY=${y}")
            }
        }


        return true
    }
}